package com.tank.demo01;

import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * https://github.com/llinzhe/tank
 * @author luna
 *
 */
public class Tank {

	int x = 200, y = 200;
	Dir dir = Dir.DOWN;
	private static final int SPEED = 5;
	
	public static final int WIDTH = ResourceMgr.goodTankD.getWidth();
	public static final int HEIGHT = ResourceMgr.goodTankD.getHeight();
	
	private Random random = new Random();
	
	private TankFrame tf = null;
	
	// 是否静止状态
	private boolean moving = true;
	private boolean living = true;
	
	Rectangle rect = new Rectangle();
	
	private Group group = Group.BAD;

	public boolean isMoving() {
		return moving;
	}

	public void setMoving(boolean moving) {
		this.moving = moving;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public Dir getDir() {
		return dir;
	}

	public void setDir(Dir dir) {
		this.dir = dir;
	}

	public static int getSpeed() {
		return SPEED;
	}
	
	public Group getGroup() {
		return group;
	}

	public void setGroup(Group group) {
		this.group = group;
	}

	public Tank(int x, int y, Dir dir , Group group,TankFrame tf) {
		super();
		this.x = x;
		this.y = y;
		this.dir = dir;
		this.group = group;
		this.tf = tf;
		
		rect.x = this.x;
		rect.y = this.y;
		rect.width = WIDTH;
		rect.height = HEIGHT;
	}

	public void paint(Graphics g) {
		if (!living) {
			tf.tanks.remove(this);
		}
		
		switch (dir) {
		case LEFT:
			g.drawImage(this.group == Group.GOOD ? ResourceMgr.goodTankL:ResourceMgr.badTankL, x, y, null);
			break;
		case UP:
			g.drawImage(this.group == Group.GOOD ? ResourceMgr.goodTankU:ResourceMgr.badTankU, x, y, null);
			break;
		case RIGHT:
			g.drawImage(this.group == Group.GOOD ? ResourceMgr.goodTankR:ResourceMgr.badTankR, x, y, null);
			break;
		case DOWN:
			g.drawImage(this.group == Group.GOOD ? ResourceMgr.goodTankD:ResourceMgr.badTankD, x, y, null);
			break;
		default:
			break;
		}
		move();
	}

	private void move() {
		if (!moving) return;
		switch (dir) {
		case LEFT:
			x -= SPEED;
			break;
		case UP:
			y -= SPEED;
			break;
		case RIGHT:
			x += SPEED;
			break;
		case DOWN:
			y += SPEED;
			break;
		default:
			break;
		}
		
		// 改变敌方坦克的发射，方向
		if (this.group == Group.BAD && random.nextInt(100)>95) {
			this.fire();
		}
		// 移动一次换一个方向
		if (this.group == Group.BAD && random.nextInt(100)>95) {
			randomDir();
		}
		
		boundsCheck();
		
		// 边界检测之后更新rect
		rect.x = this.x;
		rect.y = this.y;
		
	}

	/**
	 * 边界检测
	 */
	private void boundsCheck() {
		if (this.x < 0) x = 0;
		if (this.y < 30) y = 30;
		if (this.x > TankFrame.GAME_WIDTH - Tank.WIDTH) x = TankFrame.GAME_WIDTH - Tank.WIDTH;
		if (this.y > TankFrame.GAME_HEIGHT - Tank.HEIGHT) y = TankFrame.GAME_HEIGHT - Tank.HEIGHT ;
	}

	private void randomDir() {
		this.dir = Dir.values()[random.nextInt(4)];
	}

	/**
	 * 如果返回子弹，不知道要返回几颗子弹，什么类型的子弹
	 */
	public void fire() {
		int bX = this.x + Tank.WIDTH/2 - Bullet.WIDTH/2;
		int bY = this.y + Tank.HEIGHT/2 - Bullet.HEIGHT/2;
		
		tf.bullets.add(new Bullet(bX, bY, dir,this.group,this.tf));
	}

	/**
	 * 坦克die
	 */
	public void die() {
		this.living = false;
	}

}
